//
//  AppDelegate.swift
//  SinaWeibo
//
//  Created by 程建 on 16/9/22.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        registerNotification()
        //设置跟视图控制器
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.backgroundColor = UIColor.white
        //设置根视图控制器
        
        window?.rootViewController = CJUserAccountViewModel.shardAccountViewModel.userLogin ?CJWelcomeViewController() : CJTabBarController()
        //让window成为主视图并可见
        window?.makeKeyAndVisible()
        
        return true
    }

    //希望将根视图的切换全部放在appDelegate里  可以使用通知来完成一对多逻辑操作
    //实际中不建议大量使用通知  == 阅读性变差
    //1. 注册某通知 添加观察者
    //2. 实现通知监听方法
    //3. 在需要的地方发出通知
    //4. 移除通知
    
    //注册通知
    private func registerNotification() {
        
        NotificationCenter.default.addObserver(self, selector: #selector(setRootViewController(n:)), name: NSNotification.Name(rawValue: KchangeRootViewController), object: nil)
    
    }
    
    func setRootViewController(n:NSNotification) {
        
        if n.object == nil {
            
            window?.rootViewController = CJTabBarController()
        }else {
            window?.rootViewController = CJWelcomeViewController()
        }
        
    }
    
    //移除通知,此处写移除通知没有意义,代码格式
    deinit {
        NotificationCenter.default.removeObserver(self)
    }

}

